/**  
 * @Title: SecurityUtil.java
 * @Package com.uroad.cxy.util
 * @Description: TODO(��һ�仰�������ļ���ʲô)
 * @author oupy 
 * @date 2013-6-15 ����2:07:25
 * @version V1.0  
 */
package com.apware.utils.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author Administrator
 * 
 */
public class SecurityUtil {
 
	public static String toHMACSHA256String(String test,String mykey) {
	 
		try {
			Mac mac = Mac.getInstance("HmacSHA256");
			SecretKeySpec secret = new SecretKeySpec(mykey.getBytes(),
					"HmacSHA256");
			mac.init(secret);
			byte[] digest = mac.doFinal(test.getBytes());
			return Hex.encodeHexStr(digest); 
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return "";
		}
	}
	
	/**
	 * 32位MD5
	 * @param plainText
	 * @return
	 */
	 public static String MD532(String plainText) {
	        String re_md5 = new String();
	        try {
	            MessageDigest md = MessageDigest.getInstance("MD5");
	            md.update(plainText.getBytes());
	            byte b[] = md.digest();
	 
	            int i;
	 
	            StringBuffer buf = new StringBuffer("");
	            for (int offset = 0; offset < b.length; offset++) {
	                i = b[offset];
	                if (i < 0)
	                    i += 256;
	                if (i < 16)
	                    buf.append("0");
	                buf.append(Integer.toHexString(i));
	            }
	 
//	            re_md5 = buf.toString();
	            re_md5 = buf.toString().toUpperCase();
	 
	        } catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	        }
	        return re_md5;
	    }


}
